package org.hxd.user.controller;

import org.hxd.pojo.User_inf;
import org.hxd.units.PageModel;
import org.hxd.user.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataIntegrityViolationException;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping("/user")
public class UserController {
    @Autowired
    UserService userService;
    //登录系统
    @PostMapping("/login")
    public String user_login(HttpSession session, User_inf user_inf, Model model){
        User_inf user_inf1 = userService.query_User(user_inf);
        if(user_inf1==null){
            model.addAttribute("msg","用户名或密码错误！");
            return "index";
        }else {
            session.setAttribute("user", user_inf1);
            return "jsp/main";
        }
    }
    //退出系统
    @RequestMapping("/out")
    public String out(Model model,HttpSession session){
        session.invalidate();
        model.addAttribute("msg","请重新登录！");
        return "index";
    }

    //网站首页
    @RequestMapping("/first")
    public String first(){
        return "jsp/main";
    }

    //查询用户
    @RequestMapping("/finduser")
    public String finduser(@RequestParam(defaultValue = "1") int pageIndex, User_inf user_inf,Model model){
        PageModel pageModel = new PageModel();
        pageModel.setPageIndex(pageIndex);
        int rocordCount = userService.findUserCount(user_inf);
        pageModel.setRocordCount(rocordCount);
        model.addAttribute("pageModel",pageModel);
        //查询用户信息
        List<User_inf> users  = userService.findUser(user_inf,pageModel);
        model.addAttribute("users",users);
        model.addAttribute("user",user_inf);
        return "jsp/user/user";
    }
    //修改用户信息
    @RequestMapping("/modifyUser")
    public String modifyUser(int pageIndex,User_inf user_inf,Model model,String flag){
        if(flag == null){
            User_inf user = userService.findUserByid(user_inf.getId());
            model.addAttribute("user",user);
            model.addAttribute("pageIndex",pageIndex);
            return "jsp/user/showUpdateUser";
        }else{
            int rows = userService.modifyUser(user_inf);
            if(rows>0){
                return "redirect:/user/finduser?pageIndex="+pageIndex;
            }else {
                model.addAttribute("fail","用户修改信息失败！");
                return "jsp/fail";
            }
        }
    }
    //添加用户
    @RequestMapping("/addUser")
    public String addUser(User_inf user_inf,Model model){
        int rows = userService.addUser(user_inf);
        if(rows>0){
            PageModel pageModel = new PageModel();
            int recordCount = userService.findUserCount(null);
            pageModel.setRocordCount(recordCount);
            return "redirect:/user/finduser?pageIndex="+pageModel.getTotalSize();
        }else {
            model.addAttribute("fail","用户添加信息失败！");
            return "jsp/fail";
        }
    }
    //用户删除
    @RequestMapping("/removeUser")
    public String removeUser(Integer[] ids,Model model,HttpSession session){
        User_inf user = (User_inf) session.getAttribute("user");
        for (Integer id : ids) {
            if(user.getId() == id){
                model.addAttribute("fail","不能删除当前用户自身");
                return "jsp/fail";
            }
        }
        try{
            int rows = userService.removeUser(ids);
            if(rows == ids.length){
                return "redirect:/user/finduser";
            }else {
                model.addAttribute("fail","用户删除信息失败！");
                return "jsp/fail";
            }
        }catch (DataIntegrityViolationException e){
            model.addAttribute("fail","当前用户有发布公告或文档，请删除公告或文档后在进行操作！");
            return "jsp/fail";
        }
    }
    //添加用户判断
    @ResponseBody
    @RequestMapping("/checkloginname")
    public String checkloginname(String loginname){
        User_inf user = userService.findUserByname(loginname);
        if(user!=null){
            return "EXIST";
        }else{
            return "OK";
        }
    }
}
